package com.tanglon.utils;

import com.tanglon.domain.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils
{

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        Authentication authentication = getAuthentication();
        if (authentication == null) {
            return null; // 无认证信息（未登录）
        }
        Object principal = authentication.getPrincipal();
        // 只有当 principal 是 LoginUser 类型时才返回，否则返回 null（如匿名用户）
        return (principal instanceof LoginUser) ? (LoginUser) principal : null;
    }


    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id != null && (1L == id || 14787164048663L == id);
    }

    public static Long getUserId() {
        LoginUser loginUser = getLoginUser();
        return (loginUser != null && loginUser.getUser() != null)
                ? loginUser.getUser().getId()
                : null;
    }
}
